home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Collections: MegaDisc
/
MegaDisc 38 (1994-02)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).zip
/
MegaDisc 38 (1994-02)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).adf
/
Programming
/
Introduction_To_C
/
Chapter2
/
Chapter2
Wrap
Text File
|
1994-02-09
|
4KB
|
100 lines
Chapter Two
Fundamentals of C
by Jason Lowe
38 *^* 38 *^* 38 *^* 38 *^* 38 *^* 38 *^* 38 *^* 38 *^* 38 *^* 38
How a C program is created.
When you want to program in C you need two things, a C compiler and
a text editor. With the text editor you create a text file
containing a list of C instructions (called the C source) and save
it to disk. You then use the C compiler to take this C source from
disk and create the final runable file (called the executable).
When you create the C source and save it to disk its filename must
end with '.c'. Then when you use the C compiler to create the
executable the filename of the executable will be the same as the C
source except it will have the '.c' chopped off. For example,
c source executable
-------- ----------
mysource.c --> mysource
bananas.c --> bananas
game.c --> game
pacman.c --> pacman
When you call your C compiler your C source code will go through
three stages before it becomes an executable. Firstly your C source
will be compiled into assembler source by the compiler. Then the
assembler source will be assembled into object code by the
assembler. Finally, the linker takes this object code, combines it
with the necessary libaries and out pops the executable. Here's a
visual representation of this,
+----------+ +-----------+ +--------+
source --> | Compiler | --> | Assembler | --> | Linker | --> executable
+----------+ +-----------+ +--------+
If the compiler finds any errors in your C source you must correct
these errors and then run it through the compiler again. Once there
are no errors in your C source you can then call the assembler and
the linker to create the executable. Sometimes you will come across
errors when linking but this will be discussed later on.
How to use your C compiler
A C compiler is usually run through the cli with the name of your C
source as one of the arguments. The other arguments are parameters
to how your C program is to perform.
If you are using the C compiler found on fish 508 you must use four
seprerate programs in order to convert your C source into an
executable. These programs are hcc, top, a68k and blink. In this C
compiler hcc is the compiler, both top and a68k are the assembler
and blink is the linker.
Let's suppose you want to compile the C program 't.c'. Here's how
you do it,
HCC -L t.c t.t
If hcc found no errors in 't.c' you are ready the create the
executable. Here's how you'd do this,
HCC -L t.c t.t
TOP t.c t.s
a68k t.s t.o
BLINK FROM lib:hcc.o+t.o LIB lib:hcc.lib+lib:stubs+lib:amiga.lib TO t
As you can see this is quite a bit to type in each and everytime
you want to create the executable. It's a good idea to save this as
a text file and then call the amigados command 'execute'. For
example, if you saved the above as 'link' you would simply call,
execute link
and your executable would be created.
If you are using the C compiler from the public domain libraries of
Megadisc (UT 337 A,B) you can either follow the above method or use
the two small programs 'compile' and 'compile&link'. As you might
guess the program 'compile' compiles your C source and
'compile&link' creates the executable. For example,
compile myprogram.c <-- Compile the C source 'myprogram.c'
compile&link myprogram.c <-- Create the exectuable from 'myprogram.c'
If you are using a different compiler from fish 508 consult your
own documentation to find out how to compile a program and how to
create the executable.
38 *^* 38 *^* 38 *^* 38 *^* 38 *^* 38 *^* 38 *^* 38 *^* 38 *^* 38